<template>
    <div class="sys-pos-container flex flex-col">
        <a-card class="mb-2.5">
            <common-form
                :model="state.queryForm"
                name="menu-form"
                @query="handleQuery"
                @add="handleAdd"
                @reset="handleReset"
            >
                <a-form-item
                    v-if="userInfoStore.state.userInfos.accountType == 999"
                    label="租户"
                    name="tenantId"
                    style="width: 250px;"
                >
                    <a-select v-model:value="state.queryForm.tenantId" placeholder="">
                        <a-select-option
                            v-for="item in state.tenantList"
                            :key="item.value"
                            :value="item.value">
                            {{item.label}}    
                        </a-select-option>
                    </a-select>
                </a-form-item>
                <a-form-item
                    label="职位名称"
                    name="name"
                    class="w-1/2.5"
                >
                    <a-input v-model:value="state.queryForm.name"/>
                </a-form-item>
                <a-form-item
                    label="职位编码"
                    name="code"
                    class="w-1/2.5"
                >
                    <a-input v-model:value="state.queryForm.code"/>
                </a-form-item>
            </common-form>
        </a-card>
        <a-card class="flex-1 card-table">
            <common-table 
                :columns="columns" 
                :data="state.tableData"
                :pagination="false"
                :loading="state.loading"
                @change="changePagination"
            >
                <template #bodyCell="{ column, text, record }">
                    <template v-if="column.dataIndex == 'userList'">
                        <span>{{ Array.isArray(record.userList)?record.userList.length:0 }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'userListDetail'">
                        <data-detail :data="record.userList"/>
                    </template>
                    <template v-if="column.dataIndex === 'status'">
                        <a-tag color="green" v-if="record.status == 1">启用</a-tag>
                        <a-tag color="red" v-if="record.status == 2">禁用</a-tag>
                    </template>
                    <template v-if="column.dataIndex ==='history'">
                        <div style="cursor: pointer;">
                            <edit-record-history :row="record"/>
                        </div>
                    </template>
                    <template v-if="column.dataIndex === 'operation'">
                        <div class="w-full">
                            <a-button :icon="h(EditOutlined)" ghost type="primary" class="mr-1" @click="handleEdit(record)" >编辑</a-button>
                            <a-popconfirm
                                title="确定删除?"
                                @confirm="handleDelete(record)"
                            >
                                <a-button :icon="h(DeleteOutlined)" danger >删除</a-button>
                            </a-popconfirm>
                        </div>
                    </template>
                </template>
            </common-table>
        </a-card>
        <edit-pos ref="editPosRef" @handle-query="handleQuery"/>
    </div>
</template>


<script setup lang="ts" name="sysPos">
import { onMounted,reactive,h,ref } from 'vue';
import CommonForm from '@/components/common-form/index.vue'
import CommonTable from '@/components/common-table/index.vue'
import type { ResType } from '@/api/types';
import type { TenantListType } from '../onlineUser/type';
import { useUserInfo } from '@/stores/userInfo';
import { apiSysTenantListGet } from '@/api/sys/sysTenant';
import { apiSysPosDeletePost, apiSysPosListGet } from '@/api/sys/sysPos';
import editRecordHistory from "@/components/self/edit-record-history.vue";
import { DeleteOutlined, EditOutlined } from '@ant-design/icons-vue';
import dataDetail from './components/dataDetail.vue';
import editPos from './components/editPos.vue';
import type { ItemType } from './types';
import { emitter, showMsg } from '@/utils';

defineOptions({
    name: 'sysPos'
})
const userInfoStore = useUserInfo()
const editPosRef = ref<InstanceType<typeof editPos>>()
const state =reactive({
    queryForm:{} as any,
    tableParams:{
        page: 1,
		pageSize: 10,
		total: 0 as any,
    },
    tableData:[] as ItemType[],
    loading: false,
    tenantList:[] as TenantListType,
})

const columns : any[] = [
    {
        title: '序号',
        align: "center",
        width: 60,
        customRender: ({ index }:{index:number}) => {
            return `${ index + 1 }`;
        },
        fixed: 'left',
    },
    {
        title: '职位名称',
        dataIndex: 'name',
        key: 'name',
        align: 'center',
        width: 100,
    }, 
    {
        title: '职位编码',
        dataIndex: 'code',
        key: 'code',
        align: 'center',
        width: 80,
    },     
    {
        title: '在职人数',
        dataIndex: 'userList',
        key: 'userList',
        align: 'center',
        width: 80,
    }, 
    {
        title: '人员明细',
        dataIndex: 'userListDetail',
        key: 'userListDetail',
        align: 'center',
        width: 80,
    }, 
    {
        title: '排序',
        dataIndex: 'orderNo',
        key: 'orderNo',
        align: 'center',
        width: 50,
    }, 
    {
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        align: 'center',
        width: 80,
    }, 
    {
        title: '修改记录',
        dataIndex: 'history',
        key: 'history',
        align: 'center',
        width: 80,
    }, 
    {
        title: '操作',
        align: 'center',
        key:"operation",
        dataIndex: 'operation',
        width: 150,
        fixed: 'right',
    }
]

const handleQuery = async () => {
    state.loading = true
    const res = await apiSysPosListGet(state.queryForm.name,state.queryForm.code,state.queryForm.tenantId) as ResType<ItemType[]>
    state.tableData = res.data.result
    state.loading = false
}

const handleAdd = async () => {
    editPosRef.value?.handleOpen({status: 1, tenantId: state.queryForm.tenantId, orderNo: 100})
}

const handleReset = async () => {
    state.queryForm = {}
    state.queryForm.tenantId = state.tenantList[0].value;
    await handleQuery()
}

const changePagination = async(pagination:{current:number,pageSize:number})=>{
    state.tableParams.page = pagination.current
    state.tableParams.pageSize = pagination.pageSize
    await handleQuery()
}


const handleEdit = async (row:any)=>{
    editPosRef.value?.handleOpen(row)
}

const handleDelete = async (row:any)=>{
	await apiSysPosDeletePost({id:row.id})
    showMsg('删除成功','success')
    await handleQuery()
    emitter.emit('posOrRoleUpdate')
}
onMounted(async()=>{
    if (userInfoStore.state.userInfos.accountType === 999) {
        const res = await apiSysTenantListGet() as ResType<TenantListType>;
        state.tenantList = res.data.result;
        state.queryForm.tenantId = state.tenantList[0].value;
    }
    await handleQuery()
})
</script>

<style lang="scss" scoped>
.sys-pos-container{
    .card-table{
        :deep(.ant-card-body){
            height: 100%;
        }
    }
}
:deep(.ant-table-cell-fix-right){
    // right: 0 !important;
}
:deep(.ant-btn >span) {
    display:inline-flex!important;
}
</style>